﻿using TTEngine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SCG.TurboSprite;
using SCG.General;
using SCG.TurboSprite.SquareGrid;

namespace TorchesTombs
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        //new game button clicked
        public void NewGame()
        {
            Dungeon dungeon = new Dungeon();
            DungeonTheme theme = new DungeonTheme(1);
            DungeonLevelBuilder builder = new DungeonLevelBuilder(theme, dungeon);

            _pageDungeon.Cleanup();

            DungeonLevel level = builder.BuildLevel(_pageDungeon);
            level.Visibility();
            _pageDungeon.Level = level;
            _pageDungeon.ImpulseProcessed();

            //center on party leader
            MapObject leader = Globals.Party.Members[0];
            _pageDungeon.CenterOn(leader.X, leader.Y);

            Page = PageTypes.Dungeon;
        }

        //form load
        private void Form1_Load(object sender, EventArgs e)
        {
            //create SpriteSources
            TypeLoader loaderSpriteSources = new TypeLoader("SpriteSource", "");
            foreach (Type t in loaderSpriteSources.Types)
                loaderSpriteSources.CreateInstance(t);

            //create application pages
            _pageTitle = new TitlePage();
            _pageTitle.MainForm = this;
            _pageDungeon = new DungeonPage();
            _pageDungeon.MainForm = this;

            //load title page
            Page = PageTypes.Title;

            //create the party members
            for (int n = 0; n < 3; n++)
            {
                Warrior w = new Warrior();
                if (n == 0)
                {
                    Torch torch = (Torch)w.GetGearWithTag("Torch");
                    w.ReadyGear(torch);
                }
                Globals.Party.Members.Add(w);
            }
        }

        //change pages
        public PageTypes Page
        {
            get
            {
                return _currentPageType;
            }
            set
            {
                if (_currentPage != null)
                    if (_currentPageType == value)
                        return;

                //unload old page
                if (_currentPage != null)
                {
                    _currentPage.PageUnloaded();
                    Controls.Remove(_currentPage);
                }

                //change page
                _currentPageType = value;
                switch (_currentPageType)
                {
                    case PageTypes.Title:
                        _currentPage = _pageTitle;
                        break;
                    case PageTypes.Dungeon:
                        _currentPage = _pageDungeon;
                        break;
                }

                //load new page
                _currentPage.Dock = DockStyle.Fill;
                _currentPage.Visible = true;
                Controls.Add(_currentPage);
                _currentPage.BringToFront();
                _currentPage.PageLoaded();
            }
        }

        //private members
        private BasePage _currentPage = null;
        private PageTypes _currentPageType;
        private DungeonPage _pageDungeon;
        private TitlePage _pageTitle;
    }

    public enum PageTypes { Title, Dungeon };
}
